'''
功能：转HEIC为jpg
'''

import time, os, sys
sys.path.append(os.path.dirname(__file__))
from modify_file_time import get_file_time, change_file_time
from hos import *
from common import *
from PIL import Image
import piexif #, pyheif # 先用cyg_win ./apt-cyg.sh install libheif
from pillow_heif import register_heif_opener
register_heif_opener()


def main():
    for path in argv_files():
        print("当前文件", path)
        if path.endswith(".HEIC"):
            save_path = OSrep_ext(path, ".jpg")
            create, modify, access = get_file_time(path)
            image = Image.open(path)

            # heic = pyheif.read_heif(path)
            # image = Image.frombytes(mode=heic.mode, size=heic.size, data=heic.data)

            # exif_dict = piexif.load(path) # 读不到HEIC图片的信息
            exif_dict = image.getexif()   #获取exif信息
            exif_bytes = exif_dict.tobytes()
            # 保存为同名jpg
            image.save(save_path, exif=exif_bytes)
            # piexif.insert(piexif.dump(exif_dict), save_path)
            
            change_file_time(save_path, create, modify, access)
            print(f"{path}已转换为{save_path}")


if __name__=="__main__":
    main()
